#pragma once

#include<iostream>
#include<unistd.h>
using namespace std;

typedef void(*work_t)(int);
typedef void(*task_t)(int,pid_t);//文件描述符，pid

void PrintLog(int fd,pid_t pid)
{
    cout<<"subprocess pid:"<<pid<< " ,fd:"<< fd<<" ,Task is :Print Log task"<<endl;
}
void ReloadConf(int fd,pid_t pid)
{
    cout<<"subprocess pid:"<<pid<< " ,fd:"<< fd<<" ,Task is :Reload Conf task"<<endl;
}
void ConnectMySQL(int fd,pid_t pid)
{
    cout<<"subprocess pid:"<<pid<< " ,fd:"<< fd<<" ,Task is :Connect MySQL task"<<endl;
}

task_t tasks[3] ={PrintLog,ReloadConf,ConnectMySQL};

uint32_t NextTask()
{
    return rand()%3;
}

void worker(int fd)
{
    while(1)
    {
        uint32_t commandcode = 0;
        ssize_t n = read(0,&commandcode,sizeof(commandcode));
        if(n == sizeof(commandcode))
        {
            if(commandcode >= 3) continue;
            
            tasks[commandcode](fd,getpid());
        }
        else if(n ==0)
        {
            cout<<"sub process:"<<getpid() <<" ,quit now"<<endl;
            break;
        }
        
        sleep(1);
    }
}